package com.hk.dao;

import com.hk.entity.Student;
import com.hk.entity.Tearcher;
import com.hk.utils.PropertiesUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

public class TearcherDao {
    public void add(Tearcher tearcher) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="insert into tearcher(tName,userName,pwd) values (?,?,?)";
        queryRunner.update(sql,tearcher.gettName(),tearcher.getUserName(),tearcher.getPwd());
    }
    public void delete(Integer tId) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="delete from tearcher where tId=?";
        queryRunner.update(sql,tId);
    }
    public void update(Tearcher tearcher) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="update tearcher set tName=?,pwd=?where tId=?";
        queryRunner.update(sql,tearcher.gettName(),tearcher.getPwd(),tearcher.gettId());

    }
    public List<Tearcher> list(Tearcher tearcher) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="select * from tearcher";
        List<Tearcher> list=queryRunner.query(sql,new BeanListHandler<>(Tearcher.class));
        return list;
    }
    public Tearcher findById(Integer tId) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="select * from tearcher where tId=?";
        Tearcher tearcher=queryRunner.query(sql,new BeanHandler<>(Tearcher.class),tId);
        return tearcher;
    }
    public Tearcher login(String userName,String pwd) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="select * from tearcher where userName=? and pwd=?";
        Tearcher tearcher=queryRunner.query(sql,new BeanHandler<>(Tearcher.class),userName,pwd);
        return tearcher;
    }
}
